package com.wanhe.base.model.base;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanhe.base.model.disgn.ViewItem;
import com.wanhe.base.util.Logger;
import com.wanhe.base.util.ObjectUtil;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Administrator on 2016/5/29.
 */
public class Dish implements Comparator <Dish> , Cloneable {

	private static final String TAG = Dish.class.getSimpleName();

	/**
	 * ch_dishno : 01001
	 * vch_dishname : 经济双人餐
	 * ch_typeno : 999
	 * vch_unitname : 套
	 * isSetmeal : true
	 * num_price1 : 98.00
	 * num_price2 : 0.00
	 * dishes : [{"ch_dishno":"03001
	 * ","vch_dishname":"MINI番茄郭","ch_typeno":"003","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"08014
	 * ","vch_dishname":"大白菜","ch_typeno":"008","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"03011
	 * ","vch_dishname":"酱料","ch_typeno":"003","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"05005
	 * ","vch_dishname":"锡盟草原羔羊肉","ch_typeno":"005","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"09006
	 * ","vch_dishname":"金针菇","ch_typeno":"009","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"08004
	 * ","vch_dishname":"白萝卜","ch_typeno":"008","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"03002
	 * ","vch_dishname":"海鲜贝贝郭","ch_typeno":"003","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"07016
	 * ","vch_dishname":"腐竹","ch_typeno":"007","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"06006
	 * ","vch_dishname":"蟹肉棒","ch_typeno":"006","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"02003
	 * ","vch_dishname":"加拿大草原肥牛","ch_typeno":"002","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false},{"ch_dishno":"10006
	 * ","vch_dishname":"手工面","ch_typeno":"010","vch_unitname":"份","isSetmeal":false,"num_price1":"0.00","num_price2":"0.00","dish_sizes":"[]","dish_tags":"[]","issellout":false}]
	 * issellout : false
	 */

	private String	ch_dishno;		// 餐饮系统菜品编号
	private String	vch_dishname;	// 菜品名称
	private String	ch_typeno;		// 类型编号
	private String	vch_unitname;	// 单位
	private boolean	isSetmeal;		// 是否是套餐
	private double	num_price1;		// 价格
	private double	num_price2;		// 会员价
	public String	dish_sizes;		// 规格尺寸（*1）
	public String	dishes;			// 如果是套餐，有子菜品
	public String	dish_tags;		// 口味做法（*2)
	private boolean	issellout;		// 是否卖完
	/**
	 * 该菜品在【开吃网】注册的guid
	 */
	private String	dish_guid;
	public String	dish_type_guid;	// 开吃网注册的typeID
	private String	html_desc;		// Html格式菜品描述
	private String	setitem_key;
	private String	txt1;
	/**
	 * 品项id,即套餐子菜中可换菜的id
	 */
	private String	ch_packdishuuid;
	/**
	 * 在listview中显示的时候表示是否选择
	 */
	public boolean	isChose;

	/**
	 * 菜品数量的控制属性
	 * 
	 * @author 陈德基
	 * @time 2017/7/31 13:57
	 */
	private ViewItem.CountRule countRule;

	public int tid;

	public String getTxt1() {
		return txt1;
	}

	public void setTxt1(String txt1) {
		this.txt1 = txt1;
	}

	// 卡路里
	private String calorie;

	public double getCalorie() {
		try {
			return Double.valueOf(calorie);
		} catch (Exception e) {
			Logger.e(TAG, e.getMessage());
			return 0;
		}
	}

	public void setCalorie(String calorie) {
		this.calorie = calorie;
	}

	public List <DishSize> getDish_sizesList() {
		if (!TextUtils.isEmpty(dish_sizes)) {
			return new Gson().fromJson(dish_sizes, new TypeToken <List <DishSize>>() {}.getType());
		}
		return null;
	}

	/**
	 * 如果是套餐，获得子菜品列表
	 * 
	 * @return
	 */
	public List <Dish> getChildDishList() {
		if (!TextUtils.isEmpty(dishes)) {
			return new Gson().fromJson(dishes, new TypeToken <List <Dish>>() {}.getType());
		}
		return null;
	}

	public List <DishTag> getDish_tagsList() {
		if (!TextUtils.isEmpty(dish_tags)) {
			return new Gson().fromJson(dish_tags, new TypeToken <List <DishTag>>() {}.getType());
		}
		return null;
	}

	public String getSetitem_key() {
		return setitem_key;
	}

	public void setSetitem_key(String setitem_key) {
		this.setitem_key = setitem_key;
	}

	public String getHtml_desc() {
		return html_desc;
	}

	public void setHtml_desc(String html_desc) {
		this.html_desc = html_desc;
	}

	public String getDish_guid() {
		return dish_guid;
	}

	public void setDish_guid(String dish_guid) {
		this.dish_guid = dish_guid;
	}

	public String getCh_dishno() {
		return ch_dishno;
	}

	public void setCh_dishno(String ch_dishno) {
		this.ch_dishno = ch_dishno;
	}

	public String getVch_dishname() {
		return vch_dishname;
	}

	public void setVch_dishname(String vch_dishname) {
		this.vch_dishname = vch_dishname;
	}

	public String getCh_typeno() {
		return ch_typeno;
	}

	public void setCh_typeno(String ch_typeno) {
		this.ch_typeno = ch_typeno;
	}

	public String getVch_unitname() {
		return vch_unitname;
	}

	public void setVch_unitname(String vch_unitname) {
		this.vch_unitname = vch_unitname;
	}

	public boolean isSetmeal() {
		return isSetmeal;
	}

	public void setIsSetmeal(boolean isSetmeal) {
		this.isSetmeal = isSetmeal;
	}

	public double getNum_price1() {
		return num_price1;
	}

	public void setNum_price1(double num_price1) {
		this.num_price1 = num_price1;
	}

	public double getNum_price2() {
		return num_price2;
	}

	public void setNum_price2(double num_price2) {
		this.num_price2 = num_price2;
	}

	public boolean isSellout() {
		return issellout;
	}

	public void setIssellout(boolean issellout) {
		this.issellout = issellout;
	}

	public String getCh_packdishuuid() {
		return ch_packdishuuid;
	}

	public void setCh_packdishuuid(String ch_packdishuuid) {
		this.ch_packdishuuid = ch_packdishuuid;
	}

	/**
	 * 将服务器返回的数据加工一下变成套餐的菜单真实数据
	 *
	 * @return
	 */
	public Map <String, SetMealDish> transformToSetMealDish() {
		if (isSetmeal()) {
			List <Dish> childDishList = getChildDishList();
			// 将所有的相同类型的菜品遍历然后添加到一个菜品项目中去
			// 将itemkey数据提取
			Map <String, SetMealDish> dishMap = new HashMap<>();
			if (ObjectUtil.checkObjectList(childDishList)) {
				for (int i = 0; i < childDishList.size(); i++) {
					Dish dish = childDishList.get(i);
					String dishType = getDishType(dish); // 菜品类型
					// 根据type来添加
					if (dishMap.containsKey(dishType)) {
						dishMap.get(dishType).childDish.add(dish);

					} else {
						dish.isChose = true;// 默认第一个为选中
						SetMealDish setMealDish = new SetMealDish();
						setMealDish.currentDish = dish;
						setMealDish.childDish.add(dish);
						dishMap.put(dishType, setMealDish);
					}
				}
				Map <String, SetMealDish> result = new TreeMap<>(new Comparator <String>() {
					@Override
					public int compare(String s, String t1) {
						com.orhanobut.logger.Logger.d("=====s======" + s + "===t1===" + t1);
						if (s != null && t1 != null) {
							int num1 = Integer.parseInt(s);
							int num2 = Integer.parseInt(t1);
							return num1 - num2;
						} else {
							return 0;
						}

					}
				});
				result.putAll(dishMap);
				return result;
			}
		}
		return null;
	}

	/**
	 * 获取菜品中的套餐编号
	 *
	 * @param dish
	 * @return
	 */
	public String getSetMealId(Dish dish) {
		if (ObjectUtil.checkObject(dish)) {
			String itemKey = dish.getSetitem_key();
			if (!TextUtils.isEmpty(itemKey)) {
				String setmealID = itemKey.substring(0, 6);
				return setmealID.trim();
			}
		}
		return null;
	}

	/**
	 * 获取菜品中的子菜品中的类型编号
	 *
	 * @param dish
	 * @return
	 */
	public String getDishType(Dish dish) {
		if (ObjectUtil.checkObject(dish)) {
			String itemkey = dish.getSetitem_key();
			if (!TextUtils.isEmpty(itemkey)) {
				String setmealID = itemkey.substring(6, 8);
				return setmealID.trim();
			}
		}
		return null;
	}

	/**
	 * 获取子菜品列表中的菜品的序号
	 *
	 * @param dish
	 * @return
	 */
	public String getChildDishNumber(Dish dish) {
		if (ObjectUtil.checkObject(dish)) {
			String itemkey = dish.getSetitem_key();
			if (!TextUtils.isEmpty(itemkey)) {
				String setmealID = itemkey.substring(8, 10);
				return setmealID.trim();
			}
		}
		return null;
	}

	@Override
	public int compare(Dish dish, Dish t1) {
		String num1 = dish.getChildDishNumber(dish);
		String num2 = t1.getChildDishNumber(t1);
		int number1 = Integer.parseInt(num1);
		int number2 = Integer.parseInt(num2);
		return number1 - number2;
	}

	@Override
	public Dish clone() {
		Dish dish = null;
		try {
			dish = (Dish) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return dish;
	}

	public void setCountRule(ViewItem.CountRule countRule) {
		this.countRule = countRule;
	}

	public ViewItem.CountRule getCountRule() {
		return countRule;
	}
}
